package com.bili.bili_data_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bili.bili_data_api.dao.mapper.FanjuMapper;
import com.bili.bili_data_api.dao.pojo.bangumi;
import com.bili.bili_data_api.service.FanJvService;
import com.bili.bili_data_api.util.Result;
import com.bili.bili_data_api.vo.FanJvList;
import com.bili.bili_data_api.vo.FanJvVo;
import com.bili.bili_data_api.vo.QueryVo;
import com.zaxxer.hikari.util.FastList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FanJvServiceImpl implements FanJvService {

    @Autowired
    private FanjuMapper fanjuMapper;

    @Override
    public FanJvVo getAll(QueryVo queryVo) {
        FanJvVo fanJvVo = new FanJvVo();
        QueryWrapper<bangumi> queryWrapper = new QueryWrapper<>();
        Page<bangumi> page = new Page<>(queryVo.getPageIndex(),queryVo.getPageSize());
        Page<bangumi> bangumiPage = fanjuMapper.selectPage(page, queryWrapper);
//        List<bangumi> bangumis = fanjuMapper.selectList(null);
        List<FanJvList> fanList = new ArrayList<>();
        Integer integer = fanjuMapper.selectCount(queryWrapper);
        for (bangumi bangumi : bangumiPage.getRecords()) {
            FanJvList fanJvList = new FanJvList();
            fanJvList.setId(bangumi.getId());
            fanJvList.setTitle(bangumi.getTitle());
            fanJvList.setViews(bangumi.getViews());
            fanJvList.setComment(bangumi.getComment());
            fanJvList.setFan(bangumi.getFan());
            fanJvList.setScore(bangumi.getScore());
            fanList.add(fanJvList);
        }
        fanJvVo.setTotal(integer);
        fanJvVo.setFanList(fanList);
        return fanJvVo;
    }

    @Override
    public Result edit(FanJvList fanJvVo) {
        bangumi bangumi = new bangumi();
        bangumi.setId(fanJvVo.getId());
        bangumi.setTitle(fanJvVo.getTitle());
        bangumi.setViews(fanJvVo.getViews());
        bangumi.setComment(fanJvVo.getComment());
        bangumi.setFan(fanJvVo.getFan());
        bangumi.setScore(fanJvVo.getScore());
        fanjuMapper.updateById(bangumi);

        return Result.ok();
    }

    @Override
    public Result delete(FanJvList fanJvVo) {
        fanjuMapper.deleteById(fanJvVo.getId());

        return Result.ok();
    }

    @Override
    public Result add(bangumi fanJvVo) {
        fanjuMapper.insert(fanJvVo);
        return Result.ok();
    }

    @Override
    public FanJvVo selectByTitle(QueryVo queryVo) {
        FanJvVo fanJvVo = new FanJvVo();
        LambdaQueryWrapper<bangumi> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(bangumi::getTitle,queryVo.getTitle());
        Page<bangumi> page = new Page<>(queryVo.getPageIndex(),queryVo.getPageSize());
        Page<bangumi> bangumiPage = fanjuMapper.selectPage(page, lambdaQueryWrapper);
        Integer integer = fanjuMapper.selectCount(lambdaQueryWrapper);
//        List<bangumi> bangumis = fanjuMapper.selectList(lambdaQueryWrapper);
        List<FanJvList> fastLists = new ArrayList<>();
        for (bangumi bangumi : bangumiPage.getRecords()) {
            FanJvList fanJvList = new FanJvList();
            fanJvList.setId(bangumi.getId());
            fanJvList.setTitle(bangumi.getTitle());
            fanJvList.setViews(bangumi.getViews());
            fanJvList.setComment(bangumi.getComment());
            fanJvList.setFan(bangumi.getFan());
            fanJvList.setScore(bangumi.getScore());
            fastLists.add(fanJvList);
        }
        fanJvVo.setFanList(fastLists);
        fanJvVo.setTotal(integer);
        return fanJvVo;
    }
}
